
import {post, get, dele, put} from '@/utils/serviceHelper.js';

const RISK_POSITION_LIST_LIST = "RISK_POSITION_LIST_LIST";
const RISK_POSITION_OPTIONS = "RISK_POSITION_OPTIONS";
const state = {
    riskPositionList: [],
    riskPositionPage: {page: 0, rows: 10, count: 0},
    riskPositionOptions: [],
}

const getters = {
    riskPositionList(state) {
        return state.riskPositionList;
    },
    riskPositionPage(state) {
        return state.riskPositionPage;
    },
    riskPositionOptions(state) {
        return state.riskPositionOptions;
    }
}

const actions = {
    getRiskPositionList({commit, state}, param) {
        get('/manage/position', {...state.riskPositionPage, ...param}).then((res) => {
            if (res.code == 0) {
                commit(RISK_POSITION_LIST_LIST, res);
            }
        })
    },
    getRiskPositionListOptions({commit, state}, param) {
        get('/manage/position', {...param}).then((res) => {
            if (res.code == 0) {
                commit(RISK_POSITION_OPTIONS, res);
            }
        })
    },
    getRiskPositionListOptionsReturn({commit, state}, param) {
        return new Promise((resolve, reject) => {
            get('/manage/position', param).then((res) => {
                if (res.code == 0) {
                    resolve(res.rs);
                } else {
                    reject(res.messages);
                }
            })
        });
    },
    addRiskPosition({commit, state}, param) {
        return new Promise((resolve, reject) => {
            post('/manage/position', param).then((res) => {
                if (res.code == 0) {
                    resolve(res.rs);
                } else {
                    reject(res.messages);
                }
            })
        });
    },
    editRiskPosition({commit, state}, param) {
        return new Promise((resolve, reject) => {
            put('/manage/position', param).then((res) => {
                if (res.code == 0) {
                    resolve(res.rs);
                } else {
                    reject(res.messages);
                }
            })
        });
    },
    deleteRiskPosition({commit, state}, param) {
        return new Promise((resolve, reject) => {
            dele('/manage/position/' + param.id, param).then((res) => {
                if (res.code == 0) {
                    resolve(res.rs);
                } else {
                    reject(res.messages);
                }
            })
        });
    }
}
const mutations = {
    [RISK_POSITION_LIST_LIST](state, data) {
        state.riskPositionList = data.rs;
        state.riskPositionPage.count = data.count;
    },
    [RISK_POSITION_OPTIONS](state, data) {
        state.riskPositionOptions = data.rs;
    }

}

export default {
    state,
    getters,
    actions,
    mutations
}
